package smartlifev30.com.sdk.api.gateway_app;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import smartlifev30.com.sdk.util.JsonUtil;
import smartlifev30.com.sdk.api.CallBack;
import smartlifev30.com.sdk.api.ConstantGateway;
import smartlifev30.com.sdk.bean.request.AddZoneRequest;
import smartlifev30.com.sdk.bean.request.DelZoneRequest;
import smartlifev30.com.sdk.bean.request.EditZoneRequest;
import smartlifev30.com.sdk.bean.request.QueryZoneRequest;
import smartlifev30.com.sdk.bean.request.SwitchZone;
import smartlifev30.com.sdk.bean.response.LoginResponse;
import smartlifev30.com.sdk.net.baiwei.NetManager;
import smartlifev30.com.sdk.net.baiwei.ResponseHelper;

/**
 * Created by tys on 2018/8/14.
 */

public class ZoneMgmtGatewayImpl implements IZoneMgmtGateway {

    @Override
    public void queryZone(@NonNull String userName, @NonNull String token, @NonNull String sn,
                          @Nullable List<Integer> zoneId, @NonNull CallBack<LoginResponse> callback) {
        QueryZoneRequest queryZoneRequest = new QueryZoneRequest();
        queryZoneRequest.setFrom(userName);
        queryZoneRequest.setTo(sn);
        queryZoneRequest.setMsg_id(callback.getMsgId());
        queryZoneRequest.setMsg_class(ConstantGateway.MSG_CLASS_ZONE);
        queryZoneRequest.setMsg_name("zone_query");
        queryZoneRequest.setMsg_type(ConstantGateway.MSG_TYPE_GET);
        queryZoneRequest.setToken(token);
        List<QueryZoneRequest.ZoneList> list = null;
        if (zoneId != null) {
            list = new ArrayList<>();
            for (Integer id : zoneId) {
                list.add(queryZoneRequest.new ZoneList(id));
            }
        }
        queryZoneRequest.setZone_list(list);
        NetManager.getInstance().sendMessage(JsonUtil.toString(queryZoneRequest));
        ResponseHelper.addListen(callback);
    }

    @Override
    public void addZone(@NonNull String userName, @NonNull String token, @NonNull String sn,
                        @NonNull AddZoneRequest.Zone zone, @NonNull CallBack<LoginResponse> callback) {
        AddZoneRequest addZoneRequest = new AddZoneRequest();
        addZoneRequest.setFrom(userName);
        addZoneRequest.setTo(sn);
        addZoneRequest.setMsg_id(callback.getMsgId());
        addZoneRequest.setMsg_class(ConstantGateway.MSG_CLASS_ZONE);
        addZoneRequest.setMsg_name("zone_add");
        addZoneRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        addZoneRequest.setToken(token);
        addZoneRequest.setZone(zone);
        NetManager.getInstance().sendMessage(JsonUtil.toString(addZoneRequest));
        ResponseHelper.addListen(callback);
    }

    @Override
    public void delZone(@NonNull String userName, @NonNull String token, @NonNull String sn, @Nullable List<Integer> zoneId, @NonNull CallBack<LoginResponse> callBack) {
        DelZoneRequest delZoneRequest = new DelZoneRequest();
        delZoneRequest.setFrom(userName);
        delZoneRequest.setTo(sn);
        delZoneRequest.setMsg_id(callBack.getMsgId());
        delZoneRequest.setMsg_class(ConstantGateway.MSG_CLASS_ZONE);
        delZoneRequest.setMsg_name("zone_del");
        delZoneRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        List<DelZoneRequest.ZoneList> list = null;
        delZoneRequest.setToken(token);
        if (zoneId != null) {
            list = new ArrayList<>();
            for (Integer id : zoneId) {
                list.add(delZoneRequest.new ZoneList(id));
            }
        }
        delZoneRequest.setZone_list(list);
        NetManager.getInstance().sendMessage(JsonUtil.toString(delZoneRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void editZone(@NonNull String userName, @NonNull String token, @NonNull String sn,
                         @NonNull EditZoneRequest.Zone zone, @NonNull CallBack<LoginResponse> callback) {
        EditZoneRequest editZoneRequest = new EditZoneRequest();
        editZoneRequest.setFrom(userName);
        editZoneRequest.setTo(sn);
        editZoneRequest.setMsg_id(callback.getMsgId());
        editZoneRequest.setMsg_class(ConstantGateway.MSG_CLASS_ZONE);
        editZoneRequest.setMsg_name("zone_edit");
        editZoneRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        editZoneRequest.setZone(zone);
        NetManager.getInstance().sendMessage(JsonUtil.toString(editZoneRequest));
        ResponseHelper.addListen(callback);
    }

    @Override
    public void switchZone(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull Integer zoneId, @NonNull CallBack<LoginResponse> callback) {
        SwitchZone switchZone = new SwitchZone();
        switchZone.setFrom(userName);
        switchZone.setTo(sn);
        switchZone.setMsg_id(callback.getMsgId());
        switchZone.setMsg_class(ConstantGateway.MSG_CLASS_ZONE);
        switchZone.setMsg_name("zone_switch");
        switchZone.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        switchZone.setToken(token);
        switchZone.setZone(switchZone.new Zone(zoneId));
        NetManager.getInstance().sendMessage(JsonUtil.toString(switchZone));
        ResponseHelper.addListen(callback);
    }


}
